/*
 * ESPRSSIF MIT License
 *
 * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
 *
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
 * it is free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "esp_common.h"
#include "spi_interface.h"
#include "gpio.h"
#include "spi.h"

/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABCCC
 *                A : rf cal
 *                B : rf init data
 *                C : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 user_rf_cal_sector_set(void)
{
    flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        default:
            rf_cal_sec = 0;
            break;
    }

    return rf_cal_sec;
}



LOCAL void test_task(void *pvParameters)
{
	bool ValueFromReceive = false;
	portBASE_TYPE xStatus;
	struct ip_info ip_config;
	struct station_config sta_config;
	bzero(&sta_config, sizeof(struct station_config));

	sprintf(sta_config.ssid, "TP-LINK_8F12");
	sprintf(sta_config.password, "lichao2016");

	wifi_station_set_config(&sta_config);
	wifi_get_ip_info(STATION_IF, &ip_config);
	while(ip_config.ip.addr == 0){
		vTaskDelay(1000 / portTICK_RATE_MS);
		wifi_get_ip_info(STATION_IF, &ip_config);
	}

    if (0) {
        SpiAttr attr = {SpiMode_Master,
                        SpiSubMode_0,
                        SpiSpeed_2MHz,
                        SpiBitOrder_LSBFirst};
        
        SPIInit(SpiNum_HSPI, &attr);

        
    }

    if (0) {
        GPIO_ConfigTypeDef* cfg;
        
        cfg = (GPIO_ConfigTypeDef*)zalloc(sizeof(GPIO_ConfigTypeDef));
        printf("GPIO Config\n");
        cfg->GPIO_Pin = GPIO_Pin_14;
        cfg->GPIO_Mode = GPIO_Mode_Output;
        cfg->GPIO_Pullup = GPIO_PullUp_DIS;
        cfg->GPIO_IntrType = GPIO_PIN_INTR_DISABLE;
        gpio_config(cfg);
    }
    if(1){
        spi_init();
    }
    if(1){
        bluenrg_up();
    }

	while (1) {
        if (0) {
            SpiData data;
            uint32_t buffer[10];

            buffer[0] = 5;
            buffer[1] = 0xAA;
        
            data.cmd = 0x55AA;
            data.cmdLen = 0;
            data.addr = buffer;
            data.addrLen = 0;
            data.data = buffer;
            data.dataLen = 10;
            SPIMasterSendData(SpiNum_HSPI, &data);
        }

        if (0) {
            
            printf("1111\n");
            gpio_output_set(BIT14, 0, BIT14, 0);
            //GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, GPIO_Pin_14);
            vTaskDelay(1000 / portTICK_RATE_MS);
            printf("2222\n");
            gpio_output_set(0, BIT14, BIT14, 0);
            //GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, GPIO_Pin_14);
            vTaskDelay(1000 / portTICK_RATE_MS);
        }

        if (1) {

            spi_test();
        }
        
		vTaskDelay(100 / portTICK_RATE_MS);
		printf("test_task\n");
	}
}


/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{
    printf("SDK version:%s\n", system_get_sdk_version());
    wifi_set_opmode(STATION_MODE);
    printf("set to station mode\n");

    xTaskCreate(test_task, "test_task", 512, NULL, 4, NULL);
}

